package com.example.sociamedia.interaction.mapper;

import com.example.sociamedia.interaction.entity.Notification;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface NotificationMapper {
    @Insert("INSERT INTO notifications (UserId, SenderId, Type, RelatedId, CreatedAt, IsRead) VALUES (#{userId}, #{senderId}, #{type}, #{relatedId}, NOW(), 0)")
    @Options(useGeneratedKeys = true, keyProperty = "notificationId", keyColumn = "NotificationId")
    int insertNotification(Notification notification);

    @Select("SELECT * FROM notifications WHERE UserId = #{userId} ORDER BY CreatedAt DESC")
    List<Notification> selectAllByUserId(@Param("userId") Long userId);

    @Select("SELECT * FROM notifications WHERE UserId = #{userId} AND IsRead = 0 ORDER BY CreatedAt DESC")
    List<Notification> selectUnreadByUserId(@Param("userId") Long userId);

    @Update("UPDATE notifications SET IsRead = 1 WHERE NotificationId = #{notificationId} AND UserId = #{userId}")
    int markAsRead(@Param("notificationId") Long notificationId, @Param("userId") Long userId);

    @Update("UPDATE notifications SET IsRead = 1 WHERE UserId = #{userId} AND IsRead = 0")
    int markAllAsRead(@Param("userId") Long userId);

    @Select("SELECT COUNT(*) FROM notifications WHERE UserId = #{userId} AND IsRead = 0")
    int countUnread(@Param("userId") Long userId);
} 